import { IUserSettingPromise } from "@/typing"
import useProfileInfoAction from "./useProfileInfoAction"
import { RESPONSE_CODE } from "@/common/config"
import { handleLoginCallback } from "@/common/util"
import Taro from "@tarojs/taro"
import { useEffect, useState } from "react"


const useSettingAction=()=>{
    const {  handleUserSettingAjax } = useProfileInfoAction()
   const [privacyAgreement, setPrivacyAgreement] = useState<string>("")
   const [phone, setPhone] = useState<string>("")
   const [loginBg, setLoginBg] = useState<string>("")


    const [servicAgreement, setServicAgreement] = useState<string>("")
    const handleInitSetting = async () => {
            const settingRes = await handleUserSettingAjax() as IUserSettingPromise
            const { code, msg,mobile, service_agreement, privacy_agreement,index_bg } = settingRes
            if (code === RESPONSE_CODE.SUCCESS) {
            setPhone(mobile)
            setLoginBg(index_bg)
                setPrivacyAgreement(privacy_agreement)
                setServicAgreement(service_agreement)
    
            } else if (code === RESPONSE_CODE.LOGIN_EXPIRE || code === RESPONSE_CODE.LOGIN_NEED) {
                handleLoginCallback(()=>handleInitSetting())
                // Taro.removeStorageSync(STORE_KEY.PHONE)
                // await handleInitLogin().then(async () => {
                //     handleInitSetting();
                // })
            } else {
                Taro.showToast({
                    title: msg,
                    icon: 'none'
                })
            }
    
    
        }
        const handleGoPrivacyAgreement = () => {
            Taro.navigateTo({
                url: `/pages/webview/webview?url=${privacyAgreement}`
            });
        }
        const handleGoServiceAgreement = () => {
            Taro.navigateTo({
                url: `/pages/webview/webview?url=${servicAgreement}`
            });
        }
        useEffect(()=>{
            handleInitSetting()
        },[])
    return {
        handleGoPrivacyAgreement,
        handleGoServiceAgreement,
        setPhone,
        loginBg,
        phone
    }
}

export default useSettingAction;